var prefix = jsctx + "/userGuide";

var catalogManagement = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listCatalogManagement",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            // parentId: $("#parentId").val().trim()
                            //  operateStatus: $("#operateStatus").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'content', title: '目录', width:'100',align: 'left'},
                        {
                            title: '操作',
                            field: 'roleId',
                            width:'200',align: 'center',
                            formatter: function (value, row, index) {


                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="编辑" onclick="catalogManagement.editArticle(\''
                                    + row.userGuideId
                                    + '\')">编辑</a> ';

                                var b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="删除" onclick="catalogManagement.deleteArticle(\''
                                    + row.userGuideId
                                    + '\')">删除</a> ';

                                return  b;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }


    function deleteArticle(userGuideId) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteArticle",
                type: "post",
                data: {
                    'userGuideId': userGuideId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function editArticle(userCatalogManagementId) {
        location.href = prefix + "/editArticle/" + userCatalogManagementId;
    }

    function goBack() {
        window.history.back();
    }

    function addCatalog() {
        location.href = prefix + "/addCatalog";
    }

    function addArticle() {
        location.href = prefix + "/addArticle" ;
    }

    function catalogManagement() {
        location.href = prefix + "/catalogManagement"
    }
    
    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addCatalog: addCatalog,
        addArticle: addArticle,
        editArticle:editArticle,
        deleteArticle:deleteArticle,
        catalogManagement:catalogManagement
    };
})();
